package com.gzsxy.esjy.message.netty;

import com.alibaba.fastjson.JSON;
import com.gzsxy.esjy.bus.message.InnerProtocol;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * 内部协议
 * @author zhangxiaolong
 * /heartbeat\t\n
 * String……
 * 
 *
 */
@Getter
@Setter
@Slf4j
public class Message {
	
	private final static String ERROR = "error" ;
	
	private final static String SUCCESS = "success" ;
	
	private final static String WARN = "warn" ;
	
	private String cmd ;
	
	private Object body ;
	
	private Message() {
	}
	
	
	public static Message convert(InnerProtocol protocol) {
		Message result = new Message() ;
		result.cmd = protocol.getCmd() ;
		result.body = protocol.getBody() ;
		return result ;
	}
	
	/**
	 * builder
	 * @param text
	 * @return
	 */
	public static Message builder(String text) {
		try {
			return JSON.parseObject(text, Message.class);
		}catch(Exception e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}
	
	/**
	 * 异常
	 * @param msg
	 * @return
	 */
	public static Message error(String msg) {
		Message result = new Message() ;
		result.cmd = ERROR ;
		result.body = msg ;
		return result ;
	}
	
	/**
	 * 成功
	 * @param msg
	 * @return
	 */
	public static Message success(String msg) {
		Message result = new Message() ;
		result.cmd = SUCCESS ;
		result.body = msg ;
		return result ;
	}
	
	/**
	 * 警告
	 * @param msg
	 * @return
	 */
	public static Message warn(String msg) {
		Message result = new Message() ;
		result.cmd = WARN ;
		result.body = msg ;
		return result ;
	}
	
}
